/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.swing.ImageIcon;

import org.apache.log4j.Logger;

import who.tmap.toolkit.shape.annotation.BackgroundImageShape;

/**
 * @author stl
 *
 */
public class BackgroundImageShapeImpl extends BaseAnnotationGraphicShapeImpl 
									  implements BackgroundImageShape {

    private static Logger logger = Logger.getLogger(BackgroundImageShapeImpl.class);     
    
	private File file;
	private Image image;
	private byte[] imageBytes;
	private int imageWidth =0, imageHeight =0;
	private int pixelDrawWidth, pixelDrawHeight;
	private int offsetX, offsetY;

	public BackgroundImageShapeImpl() {
		setType( BACKGROUND_IMAGE );
	}
	
	public void assignValues( BackgroundImageShape bis ) {		
		setFile( bis.getFile() );
		setImage( bis.getImage() );
		setImageBytes( bis.getImageBytes() );
		Point2D point =bis.getPt();
		
		if ( point != null ) {
			point =(Point2D)point.clone();
		}
		setPt( point );
	}	

    public int getOffsetX() {
		return offsetX;
	}

	public void setOffsetX(int offsetX) {
		this.offsetX = offsetX;
	}

	public int getOffsetY() {
		return offsetY;
	}

	public void setOffsetY(int offsetY) {
		this.offsetY = offsetY;
	}

	public void setPixelDrawHeight(int pixelDrawHeight) {
		this.pixelDrawHeight = pixelDrawHeight;
	}

	public void setPixelDrawWidth(int pixelDrawWidth) {
		this.pixelDrawWidth = pixelDrawWidth;
	}

	/**
     * @return Returns the file.
     */
    public File getFile() {
        return file;
    }
    
    public void setFile( File file ) {
    	this.file =file;
    }
    
    public int getImageWidth() {
    	return imageWidth;
    }
    
    public int getImageHeight() {
    	return imageHeight;
    }
    
    /**
     * @param file The file to load.
     */
    public void loadImageFile(File file) {
        this.file = file;
        
        image =null;
        if ( file != null ) {
	        try {
	        	imageBytes = new byte[(int)file.length()];
	        	DataInputStream in = new DataInputStream(new FileInputStream(file));
	        	in.readFully(imageBytes);
	        	in.close();	        	
	        	
		        //ImageIcon imageIcon =new ImageIcon( file.getAbsolutePath() );
	        	ImageIcon imageIcon =new ImageIcon( imageBytes );
		        image =imageIcon.getImage();
		        
		        imageWidth =image.getWidth( null );
		        imageHeight =image.getHeight( null );
		        
		        logger.debug( "image width=" + imageWidth + ", image height=" + imageHeight );
	        } catch( Exception e ) {
	        	logger.error( e );
	        }
        }
        
        if ( image == null ) {
        	width =0;
        	imageWidth =0;
        	height =0;
        	imageHeight =0;
        	bounds =null;
        }
    }
    
    public void setImageBytes( byte[] imageBytes ) {
    	this.imageBytes =imageBytes;
    }
    
    public byte[] getImageBytes() {
    	return imageBytes;
    }
    
    public Image getImage() {
    	return image;
    }
    
    public void setImage( Image image ) {
    	this.image =image;
    	if ( image != null ) {
    		imageWidth =image.getWidth( null );
    		imageHeight =image.getHeight( null );
    	} else {
    		imageWidth =0;
    		imageHeight =0;
    	}
    	
        logger.debug( "image width=" + imageWidth + ", image height=" + imageHeight );    	
    }
    
	public void draw(Graphics g) {
		if ( image != null ) {
		    Graphics2D g2d =(Graphics2D)g;	    
	        
	        g2d.drawImage( image, offsetX, offsetY, pixelDrawWidth, pixelDrawHeight, null); 
		    
		    bounds =new Rectangle2D.Double( pt.getX(), pt.getY() 
		    							  , 0d, 0d ).getBounds2D();			    
		}
	}   
	
	
	/**
	 * Nothing to fill for images.
	 */
	public void fill(Graphics g) {
    }		
	
	public void fill( Graphics g, Color color ) {
	}
	
	public boolean contains( Point2D p ) {
	    return false;
	}
	
	public boolean isInside( Rectangle2D r ) {		
		return false;
	}	
	
	public int getHandleIndexAt( Point2D p ) {
		return -1;
	}
	
	public void updateShape(int idx, Point2D p) {
	}
	
	public void moveShape( Point2D anchor, Point2D movingAnchor ) {
	}

}
